﻿using BLL;
using System;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;

namespace Thksoft.ParameterSettings
{
    public partial class SetInvestmentProject : BaseUI.BasePage
    {
        private QS_PlanDetail_DropLIstBLL dropBLL = new QS_PlanDetail_DropLIstBLL();

        /// <summary>
        /// 登录用户的公司ID
        /// </summary>
        public decimal CurrentUserCompanyID
        {
            get
            {
                return lognUser.USER_COMPANY;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCompany(lognUser.USER_COMPANY.ToString());
                //初始化绑定GridView
                SelectMethod();
            }
        }

        #region 增，修，查方法

        /// <summary>
        /// 添加公司账号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_INSERT();");
        }

        /// <summary>
        /// 修改账号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                int count = 0;
                for (int i = 0; i < this.TABLE.Rows.Count; i++)
                {
                    if (((CheckBox)this.TABLE.Rows[i].FindControl("chkSelectId")).Checked == true)
                    {
                        count++;
                        if (count == 1)
                        {
                            string pid = ((CheckBox)TABLE.Rows[i].FindControl("chkSelectId")).ToolTip;
                            MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");
                            return;
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// 查询公司账号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            SelectMethod();
        }

        /// <summary>
        /// 查询方法
        /// </summary>
        private void SelectMethod()
        {
            var dropListType = FORM_DropListType.Text;//项目类型
            var fundType = "T6";//表类型
            var cmyId = ddlCompany.Text;//公司
            string sqlStr = "SELECT * FROM QS_PLANDETAIL_DROPLIST WHERE C_TABLETYPE='" + fundType + "' AND N_CMYID=" + cmyId + " AND N_NAMETYPE=" + dropListType;
            ViewState["sqlStr"] = sqlStr;
            PageIng.CurrentPage = 1;
            BindData(false);
            int count;
            DataTable dt = dropBLL.GetData(sqlStr, null, 0, 1000, out count);
            if (dt.Rows.Count > 0)
            {
                var cmyid = dt.Rows[0]["N_CMYID"].ToString();
                BindCompany(cmyid);
            }
            TABLE.DataSource = dt;
            TABLE.DataBind();
        }

        #region 删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelele_Click(object sender, EventArgs e)
        {
            int count = 0;
            string pids = "";
            for (int i = 0; i < TABLE.Rows.Count; i++)
            {
                if (((CheckBox)TABLE.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    pids += ((CheckBox)TABLE.Rows[i].FindControl("chkSelectId")).ToolTip + ",";
                }
            }
            if (count > 0)
            {
                string strids = pids.Substring(0, pids.Length - 1);

                //int row = dropBLL.DELETE(strids, lognUser.USER_NAME);
                var row = dropBLL.NoQuery("DELETE FROM QS_PLANDETAIL_DROPLIST WHERE N_ID IN(" + strids + ")");
                if (row > 0)
                {
                    MessageBox.Show(Page, "删除成功");
                    btnQuery_Click(sender, e);
                    return;
                }
                else
                {
                    MessageBox.Show(Page, "删除失败");
                    return;
                }
            }
            else
            {
                MessageBox.Show(this, "请选择数据");
                return;
            }
        }

        #endregion 删除

        //接收一个string类型的参数,保存到str里
        public bool IsNumeric(string str)
        {
            if (str == null || str.Length == 0)    //验证这个参数是否为空
                return false;                           //是，就返回False
            ASCIIEncoding ascii = new ASCIIEncoding();//new ASCIIEncoding 的实例
            byte[] bytestr = ascii.GetBytes(str);         //把string类型的参数保存到数组里

            foreach (byte c in bytestr)                   //遍历这个数组里的内容
            {
                if (c < 48 || c > 57)                          //判断是否为数字
                {
                    return false;                              //不是，就返回False
                }
            }
            return true;                                        //是，就返回True
        }

        #endregion 增，修，查方法

        /// <summary>
        /// GridView标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindData(false);
        }

        protected void BindData(bool isFirst)
        {
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
                ViewState["sqlStr"] = null;
            }
            string sqlStr = ViewState["sqlStr"] == null ? null : (string)ViewState["sqlStr"];
            string sort = null;
            if (ViewState["SortOrder"] != null)
            {
                sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
            }
            int count;
            DataTable dt = dropBLL.GetData(sqlStr, sort, (PageIng.CurrentPage - 1) * PageIng.RowsPerPage, PageIng.RowsPerPage, out count);
            PageIng.Visible = true;
            PageIng.RowsCount = count;
            PageIng.DataSource = dt;
            PageIng.GridViewSource = TABLE;
            PageIng.RowsPerPage = PageIng._rowsPerPage;
            PageIng.Bind();
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        private void BindCompany(string cmyid)
        {
            DataTable dt = new Sys_CompanyBLL().GetInfoByPId((int)lognUser.USER_COMPANY);
            ddlCompany.DataSource = dt;
            ddlCompany.DataTextField = "CMY_NAME";
            ddlCompany.DataValueField = "CMY_ID";
            ddlCompany.DataBind();

            ddlCompany.Items.Insert(0, new ListItem("通用", "0"));
            ddlCompany.SelectedValue = cmyid;
        }
    }
}